package com.interactive1.dbutils;


public class Result
{

    public Result(int i, int j)
    {
        noRows = i;
        noCols = j;
        values = new String[noRows][noCols];
        columnNames = new String[noCols];
        currentRow = -1;
    }

    public String getColumnName(int i)
    {
        return columnNames[i];
    }

    public int getInt(int i)
        throws Exception
    {
        return Integer.parseInt(values[currentRow][i]);
    }

    public int getInt(int i, int j)
        throws Exception
    {
        return Integer.parseInt(values[i][j]);
    }

    public int getInt(int i, String s)
        throws Exception
    {
        for(int j = 0; j < noCols; j++)
            if(s.toUpperCase().equals(columnNames[j].toUpperCase()))
                return Integer.parseInt(values[i][j]);

        return 0;
    }

    public int getInt(String s)
        throws Exception
    {
        for(int i = 0; i < noCols; i++)
            if(s.toUpperCase().equals(columnNames[i].toUpperCase()))
                return Integer.parseInt(values[currentRow][i]);

        return 0;
    }

    public long getLong(int i)
        throws Exception
    {
        return Long.parseLong(values[currentRow][i]);
    }

    public long getLong(int i, int j)
        throws Exception
    {
        return Long.parseLong(values[i][j]);
    }

    public long getLong(int i, String s)
        throws Exception
    {
        for(int j = 0; j < noCols; j++)
            if(s.toUpperCase().equals(columnNames[j].toUpperCase()))
                return Long.parseLong(values[i][j]);

        return 0L;
    }

    public long getLong(String s)
        throws Exception
    {
        for(int i = 0; i < noCols; i++)
            if(s.toUpperCase().equals(columnNames[i].toUpperCase()))
                return Long.parseLong(values[currentRow][i]);

        return 0L;
    }

    public String getString(int i)
        throws Exception
    {
        return values[currentRow][i];
    }

    public String getString(int i, int j)
        throws Exception
    {
        return values[i][j];
    }

    public String getString(int i, String s)
        throws Exception
    {
        for(int j = 0; j < noCols; j++)
            if(s.toUpperCase().equals(columnNames[j].toUpperCase()))
                return values[i][j];

        return null;
    }

    public String getString(String s)
        throws Exception
    {
        for(int i = 0; i < noCols; i++)
            if(s.toUpperCase().equals(columnNames[i].toUpperCase()))
                return values[currentRow][i];

        return null;
    }

    public boolean next()
    {
        currentRow++;
        return currentRow < noRows;
    }

    public void reset()
    {
        currentRow = -1;
    }

    public int noRows;
    public int noCols;
    public String values[][];
    public String columnNames[];
    public int currentRow;
}
